package com.bukkit.gauss.InventoryFiller;


import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;

import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;

/*
 * InventoryFiller Version 1.0 - Refill your inventory
 * Copyright (C) 2011 Gauss Software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class InventoryFiller extends JavaPlugin{
	
	/**
     * Logger magic
     */
    public static final Logger log = Logger.getLogger("Minecraft");
	
    /**
     * Permission plugin
     */
    public static PermissionHandler Permissions = null;

	/**
	 * Outputs a message when disabled
	 */
	public void onDisable() {
		PluginDescriptionFile pdfFile = this.getDescription();
		log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + "disabled.");
		
	}
	
	/**
	 * Makes sure that the Permissions plugin is installed, disabling the plugin if not
	 */
	public void setupPermissions() {
		Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
		PluginDescriptionFile pdfFile = this.getDescription();
		
		if (Permissions == null) {
			if (test!= null) {
				this.getServer().getPluginManager().enablePlugin(test);
				Permissions = ((Permissions) test).getHandler();
			}
			else {
				log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + "not enabled. Permissions not detected");
				this.getServer().getPluginManager().disablePlugin(this);
			}
		}
	}
	
	/**
	 * Setup Permissions plugin
	 * Hook the events into the plugin manager
	 */
	public void onEnable() {
		
		setupPermissions();
		
		//Get the infomation from the yml file.
        PluginDescriptionFile pdfFile = this.getDescription();
        //Print that the plugin has been enabled!
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );        
	}
	
	@Override
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
        String[] split = args;
        String commandName = command.getName().toLowerCase();
        
        if (sender instanceof Player) {
            Player player = (Player) sender;
            
            if (commandName.equals("fill")) {
                
                if (split.length == 0 && Permissions.has(player, "inventoryfiller.one") ) {
                    
                	ItemStack stack = player.getItemInHand();
                	stack.setAmount(64);
                	player.setItemInHand(stack);
                	player.updateInventory();
                	
    				return true;
                 
                } 
                else if (split.length == 1 && split[0].equalsIgnoreCase("all") && Permissions.has(player, "inventoryfiller.all") ) {
                    
                	PlayerInventory inventory = player.getInventory();
                	
                	ItemStack stack = null;
                	
                	for( int i = 0; i <= 35; i++ ) { //36 slots
                		
                		stack = inventory.getItem(i);
                		
                		if( stack.getTypeId() == 0 ) continue; //Air throws an error
                		
                    	stack.setAmount(64);
                    	inventory.setItem( i, stack);
                    	
                	}
                	
                	player.updateInventory();
                	
    				return true;
    				
                }
                else if (split.length == 1 && split[0].equalsIgnoreCase("help") ) {
                    
                	player.sendMessage(ChatColor.RED + "/fill" + ChatColor.WHITE + "  -  Give 64 of the item in hand");
                	player.sendMessage(ChatColor.RED + "/fill all" + ChatColor.WHITE + "  -  Give 64 of the entire inventory");
                
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

}
